/**
 * 张志浩(作者)
 * 2018-05-17(开始日期)
 * 2018--(最终更改日期)
 * xxx(最后更改者)
 * */
//var apicom = "http://192.168.30.199:10001/";
var createdTime = globalFun.GetQueryString("t");
var p = globalFun.GetQueryString("p");
var v = globalFun.GetQueryString("v");
var oId = globalFun.GetQueryString("id");
var type = globalFun.GetQueryString("type");


//倒计时
var date = new Date($.ajax({async: false}).getResponseHeader("Date"));
// tips(createdTime);
// console.log(date);
var timer1 = date.getTime();
var time2 = (createdTime / 1) + (1800 * 1000) - timer1;

function addTimer(id1, id2, time) {
  var interval;
  if (time < 0) { //如果页面加载时已超时，直接显示订单关闭
    interval = null;
    $("#shijian").html("时间到,订单已关闭");
    return
  }
  $("#" + id1).html(Math.floor((time / 1000) / 60));
  $("#" + id2).html(getTimerString(id1, time));
  interval = setInterval(function() {
    time -= 1000;
    var s = getTimerString(id1, time) //计算秒数
    $("#" + id2).html(s);
    if (s >= 59) {
      $("#" + id1).html(Math.floor((time / 1000) / 60)); //如果秒数>=59,重新计算分钟数
    }
  }, 1000);

  function getTimerString(id, time) { //计算并返回秒数
    var m = Math.floor((time / 1000) / 60),
      s = Math.floor((time % 60000) / 1000);
    if (time > 0) {
      if (s < 10) {
        s = "0" + s
      }
      return s;
    } else { //如果已超时，显示订单关闭
      interval = null;
      $("#shijian").html("时间到,订单已关闭");
    }
  }
}
window.onload=function(){
  addTimer("minute", "second", time2);
}

$("#price").html(p)
// h5微信支付
$('.method-a').on('click', function() {
  Auth();
  // console.log(user_info)
  if (mark == 2) {
    var dataObj = {
      phone: user_info.phone,
      // phone: 13126831072,
      uuid: user_info.uuid,
      // uuid: "cd982aa6ac064f1e86087674a9d8c181",
      //orderIds: v,
      orderNums: v,
      type: 0
    }
    $.ajax({
      type: "post",
      url: apicom + "mobile/after/payOrder/sendUnifiedOrder",
      data: dataObj,
      dataType: "json",
      success: function(res) {

        if (typeof(res) == 'string') {
          res = JSON.parse(res);
        }
        if (res.code == 1) {
          var url = res.content.url;
          var dz = url + '&redirect_url=http%3a%2f%2fpjyc.sht315.com%2fhtml%2fhomePage%2fsy.html'
          window.location.replace(dz)

        } else {
          tips(res.msg)
        }
      },
      error: function(err) {
        console.log(err)
      }
    })
  } else {
    var dataObj = {
      type: type,
      orderId: oId,
      orderNums: v,
      orderMoney: p
    }
    var dataObjT = JSON.stringify(dataObj);
    console.log(dataObj)
    wxPay(dataObjT);
  }
   // console.log(dataObj)
})



// h5支付宝支付
$('.method-b').on('click', function() {
  Auth();
  if (mark == 2) {
    var data = {
      phone: user_info.phone,
      // phone: 13126831072,
      uuid: user_info.uuid,
      // uuid: "cd982aa6ac064f1e86087674a9d8c181",
      //orderIds: orderIds,
      orderNums: v,
      type: 1
    };
    $.post(apicom + "mobile/after/payOrder/sendUnifiedOrder", data, function(res) {
      if (typeof(res) == 'string') {
        res = JSON.parse(res);
      }
      if (res.code == 1) {
        var vv = JSON.parse(res.content)

        window.location.href = 'https://openapi.alipay.com/gateway.do?' + vv.body
      } else {
        tips(res.msg)
      }

    })
  } else {
    var dataObj = {
      type: type,
      orderId: oId,
      orderNums: v,
      orderMoney: p
    }
    var dataObjT = JSON.stringify(dataObj);
    AliPay(dataObjT);
  }

})


// 返回
$('.head-img').on('click', function() {
  window.history.go(-1);
})

// 调用原生微信支付
function wxPay(dataObj) {
  var ua = navigator.userAgent.toLowerCase();
  if (/pjyc_i/.test(ua)) {
    setupWebViewJavascriptBridge(function(bridge) {
      bridge.callHandler('jsWeChatpayment', dataObj, function(resp) {});
    });
  } else if (/pujiyicheng/.test(ua)) {

    window.stub.jsToWeChatpay(dataObj); //用接口stub, 通过调用内部类中的方法jsMethod给javas传回result。

  }
}

// 调用原生支付宝支付
function AliPay(dataObj) {
  var ua = navigator.userAgent.toLowerCase();
  if (/pjyc_i/.test(ua)) {
    setupWebViewJavascriptBridge(function(bridge) {
      bridge.callHandler('jsAlipaypayment', dataObj, function(resp) {});
    });
  } else if (/pujiyicheng/.test(ua)) {
    window.stub.jsToAlipay(dataObj); //用接口stub, 通过调用内部类中的方法jsMethod给javas传回result。

  }
}
var money = 100;
//支付成功失败
if (mark == 1) {
  setupWebViewJavascriptBridge(function(bridge) {
    bridge.registerHandler('StatePayment', function() {
      if (v.indexOf("PJFW") > -1) {
        locationReplace(rootdomain + 'html/service/complete.html?v=' + v + "&oId=" + oId)
        // window.location.replace(rootdomain + 'html/service/complete.html?v=' + v + "&oId=" + oId)
        //window.location.href = ;
      }

    });
  });
}
//支付成功失败
if (mark == 0) {
  function paySuccess() {
    if (v.indexOf("PJFW") > -1) {
      locationReplace(rootdomain + 'html/service/complete.html?v=' + v + "&oId=" + oId)
      //window.location.replace()
    }
  }
}
/* 替换当前页面url */
function locationReplace(url) {
  if (history.replaceState) {
    history.replaceState(null, document.title, url);
    history.go(0);
  } else {
    location.replace(url);
  }
}
/* 返回按钮 */
$("#back").click(function() {
  var from_app = globalFun.GetQueryString("from");
  // tips(from_app)
  if (from_app && from_app == "app") {
    if ($(self).hasClass("disabled")) {
      return false
    }
    $(self).addClass("disabled")
    setTimeout(function() {
      $(self).removeClass("disabled")
    }, 1000)
    if (mark === 0) {
      window.stub.goBack();
    } else if (mark === 1) {
      tips(from_app)
      setupWebViewJavascriptBridge(function(bridge) {
        bridge.callHandler('goBack',function(resp) {});
      })
    }
  } else{
    // tips(from_app)
    window.history.go(-1)
  }
})
$.init();